package liujinliang.natappvip.cc.ccall.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import liujinliang.natappvip.cc.ccall.MainActivity;
import liujinliang.natappvip.cc.ccall.R;
import liujinliang.natappvip.cc.ccall.model.Contact;
import liujinliang.natappvip.cc.ccall.view.ContactAdapter;

public class AddressFragment extends Fragment {

    private List<Contact> contactList = new ArrayList<Contact>();

    ContactAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.address_fragment, container, false);

        /**
         * 如果用户进行了授权，就读取联系人列表，并初始化
         */
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
            readContacts();
        } else {
            initFruits();
        }


        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
//        StaggeredGridLayoutManager layoutManager = new
//                StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(contactList);
        recyclerView.setAdapter(adapter);

        return view;
    }

    private void initFruits() {
        for (int i = 0; i < 2; i++) {
            Contact apple = new Contact(getRandomLengthName("Apple"), "12", R.drawable.apple_pic);
            contactList.add(apple);
            Contact banana = new Contact(getRandomLengthName("Banana"), "12", R.drawable.banana_pic);
            contactList.add(banana);
            Contact orange = new Contact(getRandomLengthName("Orange"), "12", R.drawable.orange_pic);
            contactList.add(orange);
            Contact watermelon = new Contact(getRandomLengthName("Watermelon"), "12", R.drawable.watermelon_pic);
            contactList.add(watermelon);
            Contact pear = new Contact(getRandomLengthName("Pear"), "12", R.drawable.pear_pic);
            contactList.add(pear);
            Contact grape = new Contact(getRandomLengthName("Grape"), "12", R.drawable.grape_pic);
            contactList.add(grape);
            Contact pineapple = new Contact(getRandomLengthName("Pineapple"), "12", R.drawable.pineapple_pic);
            contactList.add(pineapple);
            Contact strawberry = new Contact(getRandomLengthName("Strawberry"), "12", R.drawable.strawberry_pic);
            contactList.add(strawberry);
            Contact cherry = new Contact(getRandomLengthName("Cherry"), "12", R.drawable.cherry_pic);
            contactList.add(cherry);
            Contact mango = new Contact(getRandomLengthName("Mango"), "12", R.drawable.mango_pic);
            contactList.add(mango);
        }
    }

    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (contactList != null && contactList.size() > 0) {
            contactList.clear();
        }
    }

    private void readContacts() {
        Cursor cursor = null;

        try{

            // 查询联系人数据
            cursor = ((MainActivity)getActivity()).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    // 获取联系人姓名
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                    // 获取联系人手机号
                    String displayNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    contactList.add(new Contact(displayName, displayNum, R.drawable.apple_pic));
                }
                adapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

    }
}
